//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "LfmCovarianceProxy.h"
using namespace Cephei::QL::Legacy::Libormarketmodels;
#include <gen/QL/Legacy/Libormarketmodels/LmCorrelationModel.h>
#include <gen/QL/Math/Array.h>
#include <gen/QL/Legacy/Libormarketmodels/LmVolatilityModel.h>
#include <gen/QL/Legacy/Libormarketmodels/LfmCovarianceParameterization.h>
using namespace Cephei::QL::Math;
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Legacy::Libormarketmodels::CLfmCovarianceProxy::CLfmCovarianceProxy (Cephei::QL::Legacy::Libormarketmodels::ILmVolatilityModel^ volaModel, Cephei::QL::Legacy::Libormarketmodels::ILmCorrelationModel^ corrModel) : CLfmCovarianceParameterization(CLfmCovarianceProxy::typeid)
{
    CLmVolatilityModel^ _CvolaModel;
    CLmCorrelationModel^ _CcorrModel;
    try
    {
#ifdef HANDLE
        _phLfmCovarianceProxy = NULL;
#endif
        _CvolaModel = safe_cast<CLmVolatilityModel^> (volaModel);
        _CvolaModel->Lock();
        boost::shared_ptr<QuantLib::LmVolatilityModel>& _volaModel = static_cast<boost::shared_ptr<QuantLib::LmVolatilityModel>&> (_CvolaModel->GetShared ()); 
        _CcorrModel = safe_cast<CLmCorrelationModel^> (corrModel);
        _CcorrModel->Lock();
        boost::shared_ptr<QuantLib::LmCorrelationModel>& _corrModel = static_cast<boost::shared_ptr<QuantLib::LmCorrelationModel>&> (_CcorrModel->GetShared ()); 
        _ppLfmCovarianceProxy = new boost::shared_ptr<QuantLib::LfmCovarianceProxy> (new QuantLib::LfmCovarianceProxy ( _volaModel,  _corrModel ));
        SetLfmCovarianceParameterization (boost::dynamic_pointer_cast<QuantLib::LfmCovarianceParameterization> (*_ppLfmCovarianceProxy));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_CvolaModel != nullptr) _CvolaModel->Unlock();
        if (_CcorrModel != nullptr) _CcorrModel->Unlock();
    }
}
Cephei::QL::Legacy::Libormarketmodels::CLfmCovarianceProxy::CLfmCovarianceProxy (boost::shared_ptr<QuantLib::LfmCovarianceProxy>& childNative, Object^ owner) : CLfmCovarianceParameterization(CLfmCovarianceProxy::typeid)
{
#ifdef HANDLE
	_phLfmCovarianceProxy = NULL;
#endif
	_ppLfmCovarianceProxy = &childNative;
    _ppLfmCovarianceParameterization = new boost::shared_ptr<QuantLib::LfmCovarianceParameterization> (boost::dynamic_pointer_cast<QuantLib::LfmCovarianceParameterization> (*_ppLfmCovarianceProxy));
}
Cephei::QL::Legacy::Libormarketmodels::CLfmCovarianceProxy::CLfmCovarianceProxy (QuantLib::LfmCovarianceProxy& childNative, Object^ owner) : CLfmCovarianceParameterization(CLfmCovarianceProxy::typeid)
{
#ifdef HANDLE
	_phLfmCovarianceProxy = NULL;
#endif
	_ppLfmCovarianceProxy = new boost::shared_ptr<QuantLib::LfmCovarianceProxy> (&childNative);
    _ppLfmCovarianceParameterization = new boost::shared_ptr<QuantLib::LfmCovarianceParameterization> (boost::dynamic_pointer_cast<QuantLib::LfmCovarianceParameterization> (*_ppLfmCovarianceProxy));
    _LfmCovarianceProxyOwner = owner;
    _LfmCovarianceParameterizationOwner = owner;
}

Cephei::QL::Legacy::Libormarketmodels::CLfmCovarianceProxy::CLfmCovarianceProxy (CLfmCovarianceProxy^ copy) : CLfmCovarianceParameterization(CLfmCovarianceProxy::typeid)
{
#ifdef HANDLE
	_phLfmCovarianceProxy = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppLfmCovarianceProxy = new boost::shared_ptr<QuantLib::LfmCovarianceProxy> (copy->GetShared());
        _ppLfmCovarianceParameterization = new boost::shared_ptr<QuantLib::LfmCovarianceParameterization> (boost::dynamic_pointer_cast<QuantLib::LfmCovarianceParameterization> (*_ppLfmCovarianceProxy));
    }
}
Cephei::QL::Legacy::Libormarketmodels::CLfmCovarianceProxy::CLfmCovarianceProxy (PLATFORM::Type^ t) : CLfmCovarianceParameterization(CLfmCovarianceProxy::typeid)
{
#ifdef HANDLE
	_phLfmCovarianceProxy = NULL;
#endif
	if (!t->IsSubclassOf(CLfmCovarianceProxy::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Legacy::Libormarketmodels::CLfmCovarianceProxy::CLfmCovarianceProxy (QuantLib::Handle<QuantLib::LfmCovarianceProxy>& childNative, Object^ owner)  : CLfmCovarianceParameterization(CLfmCovarianceProxy::typeid)
{
	_phLfmCovarianceProxy = &childNative;
	_ppLfmCovarianceProxy = &static_cast<boost::shared_ptr<QuantLib::LfmCovarianceProxy>>(childNative.currentLink());
    _ppLfmCovarianceParameterization = new boost::shared_ptr<QuantLib::LfmCovarianceParameterization> (boost::dynamic_pointer_cast<QuantLib::LfmCovarianceParameterization> (*_ppLfmCovarianceProxy));
    _LfmCovarianceProxyOwner = owner;
}
Cephei::QL::Legacy::Libormarketmodels::CLfmCovarianceProxy::CLfmCovarianceProxy (QuantLib::Handle<QuantLib::LfmCovarianceProxy> childNative)  : CLfmCovarianceParameterization(CLfmCovarianceProxy::typeid)
{
	_phLfmCovarianceProxy = &childNative;
	_ppLfmCovarianceProxy = &static_cast<boost::shared_ptr<QuantLib::LfmCovarianceProxy>>(childNative.currentLink());
    _ppLfmCovarianceParameterization = new boost::shared_ptr<QuantLib::LfmCovarianceParameterization> (boost::dynamic_pointer_cast<QuantLib::LfmCovarianceParameterization> (*_ppLfmCovarianceProxy));
}
#endif
#ifdef STRUCT
Cephei::QL::Legacy::Libormarketmodels::CLfmCovarianceProxy::CLfmCovarianceProxy (QuantLib::LfmCovarianceProxy childNative)  : CLfmCovarianceParameterization(CLfmCovarianceProxy::typeid)
{
#ifdef HANDLE
	_phLfmCovarianceProxy = NULL;
#endif
	_ppLfmCovarianceProxy = new boost::shared_ptr<QuantLib::LfmCovarianceProxy> (new QuantLib::LfmCovarianceProxy (childNative));
    _ppLfmCovarianceParameterization = new boost::shared_ptr<QuantLib::LfmCovarianceParameterization> (boost::dynamic_pointer_cast<QuantLib::LfmCovarianceParameterization> (*_ppLfmCovarianceProxy));
}
#endif

Cephei::QL::Legacy::Libormarketmodels::CLfmCovarianceProxy::~CLfmCovarianceProxy ()
{
    if (_ppLfmCovarianceProxy != NULL)
    {
	    delete _ppLfmCovarianceProxy;
        _ppLfmCovarianceProxy = NULL;
    }
}
Cephei::QL::Legacy::Libormarketmodels::CLfmCovarianceProxy::!CLfmCovarianceProxy ()
{
    if (_ppLfmCovarianceProxy != NULL)
    {
	    delete _ppLfmCovarianceProxy;
    }
}
QuantLib::LfmCovarianceProxy& Cephei::QL::Legacy::Libormarketmodels::CLfmCovarianceProxy::GetReference ()
{
    if (_ppLfmCovarianceProxy == NULL) throw REFNEW NativeNullException ();
	return **_ppLfmCovarianceProxy;
}
boost::shared_ptr<QuantLib::LfmCovarianceProxy>& Cephei::QL::Legacy::Libormarketmodels::CLfmCovarianceProxy::GetShared ()
{
    if (_ppLfmCovarianceProxy == NULL) throw REFNEW NativeNullException ();
	return *_ppLfmCovarianceProxy;
}
QuantLib::LfmCovarianceProxy* Cephei::QL::Legacy::Libormarketmodels::CLfmCovarianceProxy::GetPointer ()
{
    if (_ppLfmCovarianceProxy == NULL) throw REFNEW NativeNullException ();
	return &**_ppLfmCovarianceProxy;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::LfmCovarianceProxy>& Cephei::QL::Legacy::Libormarketmodels::CLfmCovarianceProxy::GetHandle ()
{
	if (_phLfmCovarianceProxy == NULL)
	{
		_phLfmCovarianceProxy = new Handle<QuantLib::LfmCovarianceProxy> (*_ppLfmCovarianceProxy);
	}
	return *_phLfmCovarianceProxy;
}
#endif
bool Cephei::QL::Legacy::Libormarketmodels::CLfmCovarianceProxy::HasNative () 
{
	return (_ppLfmCovarianceProxy != NULL);
}

Cephei::QL::Legacy::Libormarketmodels::ILmCorrelationModel^ Cephei::QL::Legacy::Libormarketmodels::CLfmCovarianceProxy::CorrelationModel::get ()
{
    try
    {
    	boost::shared_ptr<QuantLib::LmCorrelationModel> _rv = (boost::shared_ptr<QuantLib::LmCorrelationModel>)(*_ppLfmCovarianceProxy)->correlationModel ( );   
        Cephei::QL::Legacy::Libormarketmodels::CLmCorrelationModel^ _nrv = REFNEW Cephei::QL::Legacy::Libormarketmodels::CLmCorrelationModel (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Legacy::Libormarketmodels::CLfmCovarianceProxy::IntegratedCovariance (UInt64 i, UInt64 j, Double t, Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Math::IArray^>^ x)
{
    CArray^ _Cx;
    try
    {
        QuantLib::Size _i = (QuantLib::Size)ValueHelper::Convert (i); //a
        QuantLib::Size _j = (QuantLib::Size)ValueHelper::Convert (j); //a
        QuantLib::Time _t = (QuantLib::Time)ValueHelper::Convert (t); //a
        if (Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Math::IArray^>::IsSome::get (x))
        {
            _Cx = safe_cast<CArray^> (x->Value);
            _Cx->Lock();
        }
        QuantLib::Array& _x = 
            (Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Math::IArray^>::IsSome::get (x) ? static_cast<QuantLib::Array&> (_Cx->GetReference ()) : QuantLib::Null<QuantLib::Array>()); //6
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppLfmCovarianceProxy)->integratedCovariance ( _i,  _j,  _t,  _x );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cx != nullptr) _Cx->Unlock();
    }
}
Cephei::QL::Legacy::Libormarketmodels::ILmVolatilityModel^ Cephei::QL::Legacy::Libormarketmodels::CLfmCovarianceProxy::VolatilityModel::get ()
{
    try
    {
    	boost::shared_ptr<QuantLib::LmVolatilityModel> _rv = (boost::shared_ptr<QuantLib::LmVolatilityModel>)(*_ppLfmCovarianceProxy)->volatilityModel ( );   
        Cephei::QL::Legacy::Libormarketmodels::CLmVolatilityModel^ _nrv = REFNEW Cephei::QL::Legacy::Libormarketmodels::CLmVolatilityModel (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Legacy::Libormarketmodels::ILfmCovarianceProxy^ Cephei::QL::Legacy::Libormarketmodels::CLfmCovarianceProxy_Factory::Create (Cephei::QL::Legacy::Libormarketmodels::ILmVolatilityModel^ volaModel, Cephei::QL::Legacy::Libormarketmodels::ILmCorrelationModel^ corrModel)
{
    return REFNEW CLfmCovarianceProxy ( volaModel,  corrModel);
}
